import os, logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

from log_cat import log_utils

# define of handlers, which are actions
template_dir = 'templates'

class GetClientInfoHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Cache-Control'] = 'no-cache'
        self.response.headers['Content-Type'] = 'text/xml'
        try:
            path = os.path.join(template_dir, 'client_info.xml')
            client_info = log_utils.getClientInfo(self.request)
            template_values = {'ip_info':client_info[0], 'browser_info':client_info[1]}
            self.response.out.write(template.render(path, template_values))
        except Exception, msg:
            logging.error(msg)
    def post(self):
        get(self)

class RegisterHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Cache-Control'] = 'no-cache'
        self.response.headers['Content-Type'] = 'text/xml'
        resultCode = 2
        try:
            username = self.request.get('username')
            password = self.request.get('password')
            email = self.request.get('email')
            
            if(username and password and email):
                
            else:
                resultCode = 3
            path = os.path.join(template_dir, 'client_info.xml')
            client_info = log_utils.getClientInfo(self.request)
            template_values = {'ip_info':client_info[0], 'browser_info':client_info[1]}
            self.response.out.write(template.render(path, template_values))
        except Exception, msg:
            logging.error(msg)
    def post(self):
        get(self)
        
url_prefix = '/actions/'
url_entries = [
    (url_prefix+'get-client-info/', GetClientInfoHandler),
    (url_prefix+'register/', RegisterHandler),
]
application = webapp.WSGIApplication(url_entries, debug=True)

def start():
    run_wsgi_app(application)

if __name__ == '__main__':
    start()
